package com.jelly.config.quartz;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * @创建人 ql
 * @创建时间 2018-09-26 18:19
 * @项目名称 jelly
 * @功能描述: 定时任务运行工厂类
 **/
@Configuration
public class StartSchedulerListener  implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    public MySchedulerFactory mySchedulerFactory;

    // springboot 启动监听
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        try {
            mySchedulerFactory.scheduleJobs();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

    }

    //注入SchedulerFactoryBean
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        return schedulerFactoryBean;
    }


}
